#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "qpushbutton.h"
#include "qpixmap.h"
#include<QFileDialog>
#include<QImage>
#include<QDebug>
#include<QLineEdit>

#include "getpost.h"

#include <QJsonDocument>
#include <QJsonObject>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    QIcon icon;
    //设置 消息图标
    icon.addFile(":/tubiao/xiaoxi.png");
    ui->xiaoxi->setIcon(icon);
    //设置 好友图标
    icon.addFile(":/tubiao/haoyou.png");
    ui->haoyou->setIcon(icon);
    //设置 群聊图标
    icon.addFile(":/tubiao/qunliao.png");
    ui->qunliao->setIcon(icon);
    //设置 添加好友图标
    icon.addFile(":/tubiao/addhaoyou.png");
    ui->addhaoyou->setIcon(icon);
    //设置 添加群聊图标
    icon.addFile(":/tubiao/addhaoyou.png");
    ui->addqunliao->setIcon(icon);
    //设置 头像图片
    QPixmap avatarPixmap(":/tubiao/touxiang.jpg");
    ui->touxiang->setIcon(QIcon(avatarPixmap));
    ui->touxiang->setIconSize(QSize(81, 52)); // 设置图标的大小

    ui->setTx->setIcon(QIcon(avatarPixmap));
    ui->setTx->setIconSize(QSize(150, 150));

    //设置 设置图标
    icon.addFile(":/tubiao/shezhi.png");
    ui->shezhi->setIcon(icon);

    //在第三个页面添加搜索框
   // QLineEdit *searchEdit = new QLineEdit(ui->stackedWidget->widget(3));//第三个页面索引为3
    // searchEdit->setGeometry(10,10,200,30);

    // 设置默认显示统计分析
    ui->stackedWidget->setCurrentIndex(0);
      //updateStatistics();

connect(ui->xiaoxi, &QPushButton::clicked, this, &MainWindow::switchPage);
connect(ui->haoyou, &QPushButton::clicked, this, &MainWindow::switchPage);
connect(ui->qunliao, &QPushButton::clicked, this, &MainWindow::switchPage);
connect(ui->addhaoyou, &QPushButton::clicked, this, &MainWindow::switchPage);
connect(ui->addqunliao, &QPushButton::clicked, this, &MainWindow::switchPage);
connect(ui->shezhi, &QPushButton::clicked, this, &MainWindow::switchPage);
connect(ui->setTx,&QPushButton::clicked,this,&MainWindow::openFolderDialog);

//
getPost = new GetPost(this);

//    connect(getPost, &GetPost::connected, this, &MainWindow::onConnected);
//    connect(getPost, &GetPost::disconnected, this, &MainWindow::onDisconnected);
//    connect(getPost, &GetPost::messageSent, this, &MainWindow::onMessageSent);
//    connect(getPost, &GetPost::messageReceived, this, &MainWindow::onMessageReceived);
//    connect(getPost, &GetPost::errorOccurred, this, &MainWindow::onErrorOccurred);

}

MainWindow::~MainWindow()
{
    delete ui;
}



//翻页设置
void MainWindow::switchPage() {
    QPushButton *button = qobject_cast<QPushButton*>(sender()); // 获取发送信号的按钮
    if (button == ui->xiaoxi)
        ui->stackedWidget->setCurrentIndex(0); // 设置为第一个页面
    else if (button == ui->haoyou)
        ui->stackedWidget->setCurrentIndex(1); // 设置为第二个页面
    else if (button == ui->qunliao)
        ui->stackedWidget->setCurrentIndex(2); // 设置为第三个页面
    else if (button == ui->addhaoyou)
        ui->stackedWidget->setCurrentIndex(3); // 设置为第四个页面
    else if (button == ui->addqunliao)
        ui->stackedWidget->setCurrentIndex(4); // 设置为第五个页面
    else if (button == ui->shezhi)
        ui->stackedWidget->setCurrentIndex(5); // 设置为第六个页面
}



// 将发送的消息打包成 JSON
void MainWindow::sendJsonMessage(const QString& message)
{
    QJsonObject json;
    json["order"] = "chat";
    json["sender_qq"] = "85652135";
    json["receiver_qq"] = "12332152";
    json["message"] = message;

    QJsonDocument doc(json);
    QByteArray jsonData = doc.toJson(QJsonDocument::Compact);

    getPost->sendMessage(QString::fromUtf8(jsonData));
}

void MainWindow::on_xiaoxi_clicked()
{

}


void MainWindow::on_haoyou_clicked()
{

}


void MainWindow::on_qunliao_clicked()
{

}


void MainWindow::on_addhaoyou_clicked()
{

}


void MainWindow::on_addqunliao_clicked()
{

}

void MainWindow::on_touxiang_clicked()
{




}


void MainWindow::on_shezhi_clicked()
{

}





void MainWindow::on_setTx_clicked()
{
//    QPixmap avatarPixmap(":/tubiao/touxiang.jpg");
//    ui->setTx->setIcon(QIcon(avatarPixmap));
//    ui->setTx->setIconSize(QSize(150, 150)); // 设置图标的大小



}

//更换头像
void MainWindow::openFolderDialog(){
    QString selectedImagePath = QFileDialog::getOpenFileName(this, "选择图片", "", "Image Files (*.jpg *.png *.bmp)");
        if (!selectedImagePath.isEmpty())
        {
            QImage avatarImage(selectedImagePath);
            QPixmap avatarPixmap = QPixmap::fromImage(avatarImage);
            ui->touxiang->setIcon(QIcon(avatarPixmap));
             ui->setTx->setIcon(QIcon(avatarPixmap));
        }

}






//void MainWindow::onConnected()
//{
//    // 处理连接成功的操作
//}

//void MainWindow::onDisconnected()
//{
//    // 处理断开连接的操作
//}

//void MainWindow::onMessageSent()
//{
//    // 处理消息发送成功的操作
//}

//void MainWindow::onMessageReceived(const QString &message)
//{
//    // 处理接收到消息的操作
//}

//void MainWindow::onErrorOccurred(const QString &errorMessage)
//{
//    // 处理错误的操作
//}

void MainWindow::on_MessageToSend_clicked()
{
    getPost->connectToServer("192.168.1.95", 1234);
  //  getPost->sendMessage("Your message to send");
    sendJsonMessage("This is a JSON message");
}

